﻿<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>解析json</title>
    <script src="jquery-3.3.1.min.js"></script>
    <style>

        div {
            float: left;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <button id="firstbtn">解析该json</button>
    <button id="btn2">解析该json一级目录</button>
    <button id="btn3">解析该json一二级目录</button>
    <button id="btn4">解析该json一二三级目录</button>
    <div id="result1">
        <h3>解析结果1</h3>
    </div>
    <div id="result2">
        <h3>解析结果2</h3>
    </div>
    <div id="result3">
        <h3>解析结果3</h3>
    </div>
    <div id="result4">
        <h3>解析结果4</h3>
    </div>
</body>
<script>
    menujson = [
        {
            "id": 1350,
            "pid": 1325,
            "text": "一级目录A",
            "state": "closed",
            "iconCls": "fa fa-heartbeat",
            "url": "",
            "data": [
                {
                    "id": 1350,
                    "pid": 1325,
                    "text": "二级目录AA",
                    "state": "closed",
                    "iconCls": "fa fa-heartbeat",
                    "url": "",
                    "data": [
                        {
                            "id": 1350,
                            "pid": 1325,
                            "text": "三级目录AAA",
                            "state": "closed",
                            "iconCls": "fa fa-heartbeat",
                            "url": ""

                        },
                        {
                            "id": 1350,
                            "pid": 1325,
                            "text": "三级目录AAB",
                            "state": "closed",
                            "iconCls": "fa fa-heartbeat",
                            "url": ""
                        }
                    ]
                },
                {
                    "id": 1350,
                    "pid": 1325,
                    "text": "二级目录AB",
                    "state": "closed",
                    "iconCls": "fa fa-heartbeat",
                    "url": ""
                }
            ]
        },
        {
            "id": 1350,
            "pid": 1325,
            "text": "一级目录B",
            "state": "closed",
            "iconCls": "fa fa-heartbeat",
            "url": "",
            "data": [
                {
                    "id": 1350,
                    "pid": 1325,
                    "text": "二级目录BA",
                    "state": "closed",
                    "iconCls": "fa fa-heartbeat",
                    "url": "",
                    "data": [
                        {
                            "id": 1350,
                            "pid": 1325,
                            "text": "三级目录BAA",
                            "state": "closed",
                            "iconCls": "fa fa-heartbeat",
                            "url": ""

                        },
                        {
                            "id": 1350,
                            "pid": 1325,
                            "text": "三级目录BAB",
                            "state": "closed",
                            "iconCls": "fa fa-heartbeat",
                            "url": ""
                        }
                    ]
                },
                {
                    "id": 1350,
                    "pid": 1325,
                    "text": "二级目录BB",
                    "state": "closed",
                    "iconCls": "fa fa-heartbeat",
                    "url": ""
                }
            ]
        }
    ]
    $(function () {
        $("#firstbtn").click(function () {
            for (var a in menujson) {
                $("#result1").append("</br>" + a + "</br>" + menujson[a] + "<br>")
            }
            //for (var x in menujson) {
            //         if (menujson[x] != null && typeof menujson[x] == 'object') {
            //             for(var i in )
            //         }

            //     }
        })
        //一级目录
        $("#btn2").click(function () {
            for (var a in menujson) {
                for (var b in menujson[a]) {
                    if (menujson[a][b] != null && typeof menujson[a][b] == 'object') {
                        $("#result2").append("</br>" + b + ":</br>" + menujson[a][b] + "<br>")
                    }
                    else {
                        $("#result2").append("</br>" + b + ":</br>" + menujson[a][b] + "<br>")
                    }
                }
            }
        })
        //一二级目录
        $("#btn3").click(function () {
            for (var a in menujson) {
                for (var b in menujson[a]) {
                    if (menujson[a][b] != null && typeof menujson[a][b] == 'object') {
                        for (var c in menujson[a][b]) {
                            for (var d in menujson[a][b][c])
                                if (menujson[a][b][c][d] != null && typeof menujson[a][b][c][d] == 'object') {
                                }
                                else {
                                    $("#result3").append("</br>" + d + ":</br>" + menujson[a][b][c][d] + "<br>")
                                }
                        }
                    }
                    else {
                        $("#result3").append("</br>" + b + ":</br>" + menujson[a][b] + "<br>")
                    }
                }
            }
        })
        //一二三级目录
        $("#btn4").click(function () {
            for (var a in menujson) {
                for (var b in menujson[a]) {
                    if (menujson[a][b] != null && typeof menujson[a][b] == 'object') {
                        for (var c in menujson[a][b]) {
                            for (var d in menujson[a][b][c]) {
                                if (menujson[a][b][c][d] != null && typeof menujson[a][b][c][d] == 'object') {
                                    for (var e in menujson[a][b][c][d]) {
                                        for (var f in menujson[a][b][c][d][e]) {
                                            $("#result4").append("</br>" + f + ":</br>" + menujson[a][b][c][d][e][f] + "<br>")
                                        }

                                    }
                                }
                                else {
                                    $("#result4").append("</br>" + d + ":</br>" + menujson[a][b][c][d] + "<br>")
                                }
                            }
                        }
                    }
                    else {
                        $("#result4").append("</br>" + b + ":</br>" + menujson[a][b] + "<br>")
                    }
                }
            }
        })
    })




    //$(function () {
    //    $("#firstbtn").click(function () {
    //        $("#btntwo").hide(2000);
    //        $.ajax({
    //            type: "POST",
    //            url: "http://120.27.32.237:83/common.ashx?method=getlist",
    //            dataType: "json",
    //            data: '',
    //            success: function (data) {
    //                for (var x in data) {
    //                    if (data[x] != null && typeof data[x] == 'object') {
    //                        for (var i = 0; i < data.rows.length; i++) {
    //                            $("#result").append("</br> 姓名 ：" + data.rows[i].Name + " ; 年龄 ：" + data.rows[i].Age + " ; 时间 ：" + data.rows[i].Time + "</br>");
    //                        }
    //                    }
    //                    else {
    //                        $("#result").append("</br>" + "Key = " + x + " Value = " + data[x] + "</br>");
    //                    }
    //                }
    //            },
    //            error: function (err) {
    //                alert(err);
    //            }
    //        });
    //        return false;
    //    });
    //});
</script>


</html>